# for QR codes use inline
# %matplotlib inline
# qr_setting = 'url'
# qrviz_setting = 'show'
#
# for lecture use notebook
%matplotlib inline
qr_setting = None
#
%config InlineBackend.figure_format='retina'
# import libraries
import numpy as np
import matplotlib as mp
import pandas as pd
import matplotlib.pyplot as plt
import laUtilities as ut
import slideUtilities as sl
import demoUtilities as dm
import pandas as pd
from importlib import reload
from datetime import datetime
from matplotlib import animation
from IPython.display import Image
from IPython.display import display_html
from IPython.display import display
from IPython.display import Math
from IPython.display import Latex
from IPython.display import HTML;
\(A{\bf x} = {\bf b}\)¶
display(Image("images/James_Joseph_Sylvester.jpg", height = 400))
display(Image("images/Arthur_Cayley.jpg", width = 250))
HTML(u'Hamilton photo from:<a href="https://en.wikipedia.org/wiki/en:Image:Untitled04.jpg" class="extiw" title="w:en:Image:Untitled04.jpg">http://en.wikipedia.org/wiki/Image:Untitled04.jpg</a>, Public Domain, <a href="https://commons.wikimedia.org/w/index.php?curid=268041">Link</a>')
HTML(u'Cayley photo by <a href="https://en.wikipedia.org/wiki/Herbert_Rose_Barraud" class="extiw" title="en:Herbert Rose Barraud">Herbert Beraud</a> (1845–1896) - <a rel="nofollow" class="external free" href="http://www-groups.dcs.st-and.ac.uk/~history/PictDisplay/Cayley.html">http://www-groups.dcs.st-and.ac.uk/~history/PictDisplay/Cayley.html</a>, Public Domain, <a href="https://commons.wikimedia.org/w/index.php?curid=904674">Link</a>.')
Although ways of solving linear systems were known to the ancient Chinese, the 8th century Arabs, and were used to great effect by Gauss in the 1790s, the idea that the information content of a linear system should be captured in a matrix was not developed until J.J. Hamilton in 1850. He gave it the term matrix because he saw it as a sort of “womb” out of which many mathematical objects could be delivered.
Even then, it took a long time for the concept to emerge that one might want to multiply using matrices – that a matrix was an algebraic object. It was not until 1855 that Arthur Cayley came up with the “right” definition of how to multiply using matrices – the one that we use today.
Multiplying a Matrix by a Vector¶
Last lecture we studied the vector equation:
We also talked about how one can form a matrix from a set of vectors:
In particular, if we have \(n\) vectors \({\bf a_1},{\bf a_2}, ..., {\bf a_n} \in \mathbb{R}^m,\) then \(A\) will be an \(m \times n\) matrix:
We’re going to express a vector equation in a new way. We are going to say that
is the same as:
where \(A = \left[\begin{array}{cccc}\begin{array}{c}\vdots\\\vdots\\{\bf a_1}\\\vdots\\\vdots\end{array}&\begin{array}{c}\vdots\\\vdots\\{\bf a_2}\\\vdots\\\vdots\end{array}&\dots&\begin{array}{c}\vdots\\\vdots\\{\bf a_n}\\\vdots\\\vdots\end{array}\\\end{array}\right]\) and \({\bf x} = \left[\begin{array}{c}x_1\\x_2\\\vdots\\x_n\end{array}\right].\)
What we have done is to define Matrix-vector multiplication.
That is, \(A{\bf x}\) denotes multiplying a matrix and a vector, and we define it to be the same as a linear combination of the \(\{\bf a_1, a_2, \dots, a_n\}\) with weights \(x_1, x_2, \dots, x_n.\)
Here is the definition:
If \(A\) is an \(m\times n\) matrix, with columns \(\bf a_1, a_2, \dots, a_n,\) and if \({\bf x} \in \mathbb{R}^n,\) then the product of \(A\) and \(\bf x\), denoted \(A{\bf x}\), is the linear combination of the columns of \(A\) using the corresponding entries in \(x\) as weights; that is,
Notice that if \(A\) is an \(m\times n\) matrix, then \({\bf x}\) must be in \(\mathbb{R}^n\) in order for this operation to be defined. That is, the number of columns of \(A\) must match the number of rows of \({\bf x}.\)
If the number of columns of \(A\) does not match the number of rows of \({\bf x}\), then \(A{\bf x}\) has no meaning.
Matrix-Vector Multiplication Example¶
Relationship to Span¶
For \({\bf v_1}, {\bf v_2}, {\bf v_3} \in \mathbb{R}^m,\) write the linear combination \(3{\bf v_1} + 5{\bf v_2} + 7{\bf v_3}\) as a matrix times a vector.
Notice that if \(A = [{\bf a_1} \; {\bf a_2} \; ... \;{\bf a_n}]\) then \(A{\bf x}\) is some point that lies in Span\(\{{\bf a_1},{\bf a_2}, \dots ,{\bf a_n}\}\).
fig = ut.three_d_figure((5, 1), 'Ax where A = [a1 a2]',
-10, 10, -10, 10, -10, 10, figsize=(8, 8), qr=qr_setting)
plt.close()
#
v = [4.0, 4.0, 2.0]
u = [-4.0, 3.0, 1.0]
A = np.array([v, u]).T
x = np.array([-0.1, -0.75]).T
Ax = A @ x
fig.text(v[0], v[1], v[2], r'$\bf a_1$', 'a_1', size=18)
fig.text(u[0], u[1], u[2], r'$\bf a_2$', 'a_2', size=18)
fig.text(Ax[0], Ax[1], Ax[2], r'$A\bf x$', 'Ax', size=18)
fig.text(1, -4, -10, r'Span{$\bf a_1,a_2$}', 'Span{a_1, a_2}', size=16)
# fig.text(0.2, 0.2, -4, r'$\bf 0$', '0', size=20)
# plotting the span of v
fig.plotSpan(u, v, 'Green')
fig.plotPoint(u[0], u[1], u[2], 'r')
fig.plotPoint(v[0], v[1], v[2], 'r')
fig.plotPoint(0, 0, 0, 'b')
fig.plotPoint(Ax[0], Ax[1], Ax[2], 'b')
# plotting the axes
fig.plotIntersection([0,0,1,0], [0,1,0,0])
fig.plotIntersection([0,0,1,0], [1,0,0,0])
fig.plotIntersection([0,1,0,0], [1,0,0,0])
fig.set_title(r'$A{\bf x}$ where $A = \left[\bf a_1\;a_2\right]$',
'Ax where A = [a_1 a_2]',
size=14)
fig.ax.view_init(azim = 0, elev = 22)
fig.save()
#
def anim(frame):
fig.ax.view_init(azim = frame, elev = 22)
# fig.canvas.draw()
#
# create and display the animation
HTML(animation.FuncAnimation(fig.fig, anim,
frames = 2 * np.arange(180),
fargs = None,
interval = 30,
repeat = False).to_jshtml(default_mode = 'loop'))